<?php
namespace app\beautiful\controller\admin;
use app\BaseController;
use app\common\logic\DataLogic;
use app\beautiful\model\LookModel;
use app\beautiful\model\mall\GoodsModel;

class Look extends BaseController{
    protected $name='相册';
    
    public function index()
    {
        $info=$this->request->param('info');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];
        if(!empty($info)) $where[]=['info','=',$info];

        $look_model= new LookModel();
        $order=['update_time'=>'desc','look_id'=>'desc'];
        $field="*";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($look_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function show()
    {
        $look_id=$this->request->param('look_id/d',0);
        if($look_id==0) return $this->success([]);

        if(null === $look= LookModel::find($look_id)) return $this->error($this->name.'不存在');
        if($look->is_delete!=0) return $this->error($this->name.'不存在');
        if($look->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        $detail=[
            'photos'=>$look->photos,
            'info'=>$look->info,
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function save()
    {
        $look_id=$this->request->param('look_id/d',0);
        $photos=$this->request->param('photos/a',[]);
        $info=$this->request->param('info');

        //判断传来的参数
        if(empty($photos) || !is_array($photos) || count($photos)<=0) return $this->error('请上传图片');
        if(count($photos) > 9) return $this->error('上传的图片数量不能超过九个');
       
        if(empty($info)) return $this->error('请输入文字说明');

        //操作
        if($look_id != 0){
            if(null === $look= LookModel::find($look_id)) return $this->error($this->name.'不存在');
            if($look->is_delete!=0) return $this->error($this->name.'不存在');
            if($look->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
            $pre_photos = $look->photos;
        }else{
            $look = new LookModel();
            $look->shop_id = $this->request->shopId;
            $pre_photos = [];
        }
        $look->photos = $photos;
        $look->info=$info;
        $look->update_time=$this->request->time();
        $look->operate_name= $this->request->account->username;
        $look->operate_mobile= $this->request->account->mobile;
        $look->save();

        //图片操作
        arrayNoUseImg($pre_photos); arrayUseImg($photos);
        return $this->success('保存成功');
    }

    public function del()
    {
        $look_id=$this->request->param('look_id/d',0);

        if(null === $look= LookModel::find($look_id)) return $this->error($this->name.'不存在');
        if($look->is_delete!=0) return $this->error($this->name.'不存在');
        if($look->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        //其他判断后面补充

        $look->is_delete=1;
        $look->save();
        return $this->success('删除成功');
    }
}